package com.tfr.rabbitmq.service;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import com.tfr.rabbitmq.entity.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.autoconfigure.integration.IntegrationProperties;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Description: 消费者类的方法上通过@RabbitListener注解用于监听指定名称的消息队列，
 * 而标注在方法上的@RabbitHandler则用于指示从指
 * @ClassName: MsgService
 * @Author: Rain
 * @Date: 2021-10-19 15:20
 * @Version: 1.0
 **/

@Service
//@RabbitListener(queues = "item_queue")
public class MsgService {
    
    @RabbitHandler
    public void getMsg(String msg){
        System.out.println(msg);
    }
    
    @RabbitHandler
    public void getEntity(Test msg, Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag,false);
        System.out.println(msg);
        if (deliveryTag>3){
            //拒收 ,b1 false 就是丢弃
            channel.basicNack(deliveryTag,false,true);
//            channel.basicReject();
        }
    }
}